//
//  TTNetwokingModel.swift
//  TTWeibo
//
//  Created by 陶宏路 on 16/7/22.
//  Copyright © 2016年 陶路路. All rights reserved.
//

import UIKit

class TTNetwokingModel: NSObject,NSCoding{

    
    var access_token : String?
    
    var uid:String?
    
    var screen_name:String?
    
    var avatar_large:String?
    
    var created_at: NSDate?
    
    var expires_in: NSTimeInterval = 0{
        // 当系统使用kvc 对其身上的属性赋值的时候 会走didSet方法
        didSet{
            // 通过当前的日期+秒数 =  将要过期的日期
            created_at = NSDate(timeIntervalSinceNow: expires_in)
        }
    }

    
    init(dict:[String:AnyObject]) {
        
        super.init()
        
        setValuesForKeysWithDictionary(dict)
        
        
    }
    
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {
        
        
    }
    
    override var description: String{
        let keys = ["access_token", "created_at", "uid", "screen_name" ,"avatar_large"]
        return dictionaryWithValuesForKeys(keys).description
    }

    
    func encodeWithCoder(aCoder: NSCoder) {
        
        aCoder.encodeObject(access_token, forKey: "access_token")
        aCoder.encodeObject(uid, forKey: "uid")
        aCoder.encodeObject(screen_name, forKey: "screen_name")
        aCoder.encodeObject(avatar_large, forKey: "avatar_large")
        aCoder.encodeObject(created_at, forKey: "created_at")
    }
    
    required init?(coder aDecoder: NSCoder) {
        
        access_token = aDecoder.decodeObjectForKey("access_token") as? String
        
        uid = aDecoder.decodeObjectForKey("uid") as? String
        
        screen_name = aDecoder.decodeObjectForKey("screen_name") as? String
        
        avatar_large = aDecoder.decodeObjectForKey("avatar_large") as? String
        
        created_at = aDecoder.decodeObjectForKey("created_at") as? NSDate
        
    }

}
